package com.ckk.eurekaclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckk.eurekaclient.mapper.MarkEditionMapper;
import com.ckk.eurekaclient.mapper.MarkMapper;
import com.ckk.eurekaclient.mapper.MarkerMapper;
import com.ckk.eurekaclient.pojo.MarkEditionPo;
import com.ckk.eurekaclient.pojo.MarkPo;
import com.ckk.eurekaclient.pojo.MarkerPo;
import com.ckk.eurekaclient.service.IMarkerService;
import com.ckk.eurekaclient.vo.resp.MarkFinalRespVo;
import com.ckk.eurekaclient.vo.resp.MarkRespVo;
import com.ckk.eurekaclient.vo.resp.MarkerRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * @author zhujunjun
 * @date 2021/05/20 09:28
 **/
@Service
public class MarkerServiceImpl extends ServiceImpl<MarkerMapper, MarkerPo> implements IMarkerService {

    @Autowired
    private MarkerMapper markerMapper;

    @Autowired
    private MarkMapper markMapper;

    @Autowired
    private MarkEditionMapper markEditionMapper;

    @Override
    public List<MarkFinalRespVo> getMark() {
        List<MarkRespVo> markRespVoList = markMapper.getMark();
        return getMarkFinal(markRespVoList);
    }

    @Override
    public List<MarkFinalRespVo> getMarkByLayer(String id) {
        List<MarkRespVo> markRespVoList = markMapper.getMarkByLayer(id);
        return getMarkFinal(markRespVoList);
    }

    /**
     * 处理标记点数据的公共方法
     * @param markRespVoList
     * @return
     */
    private List<MarkFinalRespVo> getMarkFinal(List<MarkRespVo> markRespVoList) {
        List<MarkFinalRespVo> markFinalRespVoList = markRespVoList.stream().map(o -> {
            MarkFinalRespVo markFinalRespVo = new MarkFinalRespVo();
            BeanUtils.copyProperties(o, markFinalRespVo);
            markFinalRespVo.setXy(o.getXy().split(","));
            return markFinalRespVo;
        }).collect(Collectors.toList());
        return markFinalRespVoList;
    }

    @Override
    public void copyMarker() {
        List<MarkerPo> markerPoList = markerMapper.selectList(new LambdaQueryWrapper<MarkerPo>());
        markerPoList.forEach(o -> {
            MarkPo mark = new MarkPo();
            mark.setLayer(o.getMLayer());
            mark.setId(o.getMItemId());
            mark.setEdition(1);
            markMapper.insert(mark);
            MarkEditionPo markEditionPo = new MarkEditionPo();
            markEditionPo.setMarkGuid(mark.getGuid());
            markEditionPo.setXy(o.getMXy());
            markEditionPo.setEdition(1);
            markEditionPo.setPopUpContent(o.getMPopupcontent());
            markEditionPo.setPopModule(o.getMPopmodule());
            markEditionPo.setPopTitle(o.getMPoptitle());
            markEditionPo.setPopType(o.getMPoptype());
            markEditionMapper.insert(markEditionPo);
        });
    }

    @Override
    public void fillMarker() {
        //update mark m, mark_edition me set me.mark_pic = CONCAT(m.layer, '_', m.id) where m.guid = me.mark_guid
    }
}
